package org.oschina.api.comment.controller;

import org.oschina.api.BaseController;
import org.oschina.api.comment.model.Comment;
import org.oschina.api.comment.service.CommentService;
import org.oschina.api.comment.vo.CommentVo;
import org.oschina.api.report.vo.ReportVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.HtmlCleanFileter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/comment")
public class CommentController extends BaseController {

	@Autowired
	private CommentService commentService;
	
	/**
	 * 举报
	 * @param id
	 * @param content
	 * @return
	 */
	@Validate
	@RequestMapping(value="/report/{id}",method=RequestMethod.POST)
	public Result createReport
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		String content
	)
	{
		return commentService.createReport(id, content);
	}
	
	/**
	 * 返回举报详细信息
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/report/concrete/{id}",method=RequestMethod.GET)
	public ReportVo findReportConcrete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return commentService.findReportConcrete(id);
	}
	
	/**
	 * 返回举报
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/report",method=RequestMethod.GET)
	public Pager<CommentVo> findReport(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return commentService.findReport(pn);
	}
	
	/**
	 * 返回删除
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public Pager<CommentVo> findDelete(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return commentService.findDelete(pn);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete/{id}",method=RequestMethod.POST)
	public Result delete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return commentService.delete(id);
	}
	
	/**
	 * 恢复已删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/recover/delete/{id}",method=RequestMethod.POST)
	public Result recoverDelete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return commentService.recoverDelete(id);
	}
	
	/**
	 * 根据评论ID返回
	 * @param commentId
	 * @param pn
	 * @return
	 */
	@Validate
	@RequestMapping(value="/comments/{id}",method=RequestMethod.GET)
	public Pager<CommentVo> findByCommentId
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) @PathVariable Integer id,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return commentService.findByCommentId(id, pn);
	}
	
	/**
	 * 返回引用
	 * @param id
	 * @param pn
	 * @return
	 */
	@Validate
	@RequestMapping(value="/{id}/quote",method=RequestMethod.GET)
	public Pager<CommentVo> findByQuoteAndType
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) @PathVariable Integer id,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return commentService.findByQuoteAndType(id, pn);
	}
	
	/**
	 * 编辑
	 * @param comment
	 * @return
	 */
	@Validate
	@RequestMapping(value="/edit/{id}",method=RequestMethod.POST)
	public Result edit
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="content",filter={HtmlCleanFileter.class},min=5,max=10000)
		}) Comment comment
	)
	{
		return commentService.edit(comment);
	}
}